package solutions.leetcode.easy;

import solutions.lib.BaseSolution;

/**
 * @author lizhidong
 * <a href="https://leetcode-cn.com/problems/plus-one/">66. 加一</a>
 */
public class Solution66 extends BaseSolution {

    public int[] plusOne(int[] digits) {
        int p = digits.length - 1;
        int upper = (digits[p] + 1) / 10;
        digits[p] = (digits[p] + 1) % 10;
        while (upper > 0 && p > 0) {
            p--;
            upper = (digits[p] + 1) / 10;
            digits[p] = (digits[p] + 1) % 10;
        }
        if (upper == 0) {
            return digits;
        } else {
            int[] rst = new int[digits.length + 1];
            rst[0] = upper;
            System.arraycopy(digits, 0, rst, 1, digits.length);
            return rst;
        }
    }

    public static void main(String[] args) {
        Solution66 solution = new Solution66();
        printVals(solution.plusOne(new int[] {
                9, 9, 9
        }));
    }
}